import Vue from 'vue'
import Vuex from 'vuex';
import $http from "@/common/utils/request.js";
import $api from "@/common/utils/api.js";
Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
		appConfig:{},
		userInfo:{},
		baseUrl: "", //开发api,
		baseImg: "", //img服务器地址,
		// baseUrl: uni.getStorageSync('baseUrl') || "http://192.168.101.163:8080", //开发api,
		// baseImg:uni.getStorageSync('baseUrl') ? uni.getStorageSync('baseUrl')+ "/kimp1000":'http://192.168.101.163:8080/kimp1000', //img服务器地址,
		token: "",
		// token:uni.getStorageSync("powerToken")|| "",
		leftPoupShow: false,  //左侧菜单poup显示
		machineClassShow: false,
		defaultNavKey: [], //默认选中的菜单key
		navTree:[ //左侧菜单树
			{
				roomName:"南昌市机房",
				url:"南昌",
				isBottom:false,
				children:[]
			}
		]
			
	},
    mutations: {
			set_leftPoupShow(state,msg){
				state.leftPoupShow = msg
			},
			set_defaultNavKey(state,msg){
				state.defaultNavKey = msg
			},
			set_machineClassShow(state,msg){
				state.machineClassShow = msg //判断机房概览是否需要显示机房分类下内部告警
			},
			set_token(state,msg){
				state.token = msg;
				// console.log('store存token===>>',msg)
				// uni.setStorageSync('userInfo', this.form.phone);
				uni.setStorageSync("powerToken",msg)
			},
			// 设置左侧树
			set_navTree(state,msg){
				state.navTree = msg
			},
			// 设置用户信息
			set_userInfo(state,msg){
				state.userInfo = {...msg}
			},
			// 更改请求地址
			set_baseUrl(state,msg){
				state.baseUrl = msg;
				state.baseImg = state.baseUrl + '/kimp1000';
			},
			set_appConfig(state,msg){
				state.appConfig = {
					...msg
				}
			},
			// 重置token
			remove_token(state,msg){
				state.token = msg;
				state.userInfo = {}
			}
		},
    actions: {
			// 获取用户信息，并设置用户信息
			get_userInfo({commit}){
				return new Promise((resolve,reject)=>{
					$http.httpRequest({
						methods:"get",
						url:$api.userList
					}).then((res)=>{
						if(!res){
							reject(res)
							return uni.showToast({
									title: "服务器请求出错",
									icon:"none"
								})
						}
						resolve(res)
						commit('set_userInfo',res.data.user);
					}).catch((err)=>{
						reject(err)
						return uni.showToast({
								title: "服务器请求出错",
								icon:"none"
							})
					})
				})
			},
			loginOut({commit}){
				return new Promise((resolve,reject)=>{
					if(uni.getStorageSync("powerToken")){
						uni.removeStorage({
							key:"powerToken",
							success:(res)=>{
								commit("remove_token","")
								// console.log('removeStorage-success',res)
								resolve(res)
							},
							fail:(err)=> {
								// console.log('removeStorage-fail',err)
								reject(err)
							}
						})
					}else{
						resolve(true)
					}
				})
			},
			// 异步设置服务器地址
			set_baseApiSync({commit},msg){
				return new Promise((resolve,reject)=>{
					uni.setStorage({
						key:'baseUrl',
						data:msg,
						success(res) {
							commit('set_baseUrl',msg)
							resolve(res)
						},
						fail(err) {
							reject(err)
						}
					})
				})
			},
			set_appConfigAction({commit}){
				return new Promise((resolve,reject)=>{
					$http.httpRequest({
						url:$api.config,
						methods:"get"
					}).then((res)=>{
						if(!res){
							reject(null)
							return uni.showToast({
								title:'服务器请求失败',
								icon:'none'
							})
						}
						resolve(res)
						// console.log('res.data.config',res.data.config)
						commit("set_appConfig",res.data.config)
					}).catch((err)=>{
						reject(err)
						uni.showToast({
							title:"请求服务器失败",
							icon:"none"
						})
					})
				})
			}
		},
		getters:{
			doNavTree(state){
				let arrTree = JSON.parse(JSON.stringify(state.navTree))
				digui(arrTree)
				function digui(arr,level = 0,fId = null){
					if(!(arr instanceof Array)){
						throw new TypeError('function digui 接受Array')
					}
					
					arr.forEach((item,index)=>{
						 item['level'] = level // 添加level属性
						 item['fId'] = fId // 添加fId属性 ：父id
						 if(!item.isBottom){
							 item['icon']= state.baseImg + '/icon/powerSet.png';
						 }
						 item['node_id'] = fId === null ? index+'' : fId+'-'+index
					})
					if(arr.length > 0){
						level = level + 1
					}
					for(let i = 0 ;i < arr.length;i++){
						if(arr[i].children && arr[i].children.length > 0){
							digui(arr[i].children,level,arr[i].node_id)
						}
					}
					
				}
				
				return arrTree
			}
		}
})
export default store